package com.zhongyouyun.web.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.setting.dialect.Props;
import cn.hutool.setting.dialect.PropsUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.SiteListModel;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.admin.model.UserSiteModel;
import com.zhongyouyun.admin.service.SiteListService;
import com.zhongyouyun.admin.service.UserService;
import com.zhongyouyun.admin.service.UserSiteService;
import com.zhongyouyun.config.RedisConfig;
import com.zhongyouyun.other.model.TokenUtil;
import com.zhongyouyun.utils.IPUtils;
import com.zhongyouyun.utils.PropUtils;
import com.zhongyouyun.utils.RegexUtils;
import com.zhongyouyun.utils.RequestModelUtil;
import com.zhongyouyun.web.model.SsoModel;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;

/**
 * @Author: shenyi
 * @Date: 2021-12-17
 * @Description:
 */
@Controller
@RequestMapping("/web/sso")
@Api(value = "三方登录认证", tags = {"三方登录认证"})
public class SSOController {
    @Autowired
    private UserService service;

    @Autowired
    private SiteListService siteListService;

    @Autowired
    private UserSiteService userSiteService;
    @Resource
    private RedisConfig redisConfig;

    @GetMapping("/Login")
    @ApiOperation(value = "三方登录", notes = "三方登录")
    public void SsoLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Props props = PropUtils.get("properties/file");
        String webUrl = props.getStr("web.url");
        SsoModel ssoModel = RequestModelUtil.getRequestConvertBean(request, SsoModel.class);
        String ipAddr = IPUtils.getIpAdrress(request);

        System.out.println(ObjectUtil.isNotEmpty(ssoModel.getAppid()));
        if (ObjectUtil.isNotEmpty(ssoModel)&&ObjectUtil.isNotEmpty(ssoModel.getAppid())) {
            System.out.println(ssoModel.getCode());
            Object object = redisConfig.get(ssoModel.getCode());
            if (ObjectUtil.isNotEmpty(object)) {
                Claims b = TokenUtil.getTokenBody(object.toString());
                if (ObjectUtil.isNotEmpty(b)) {
                    String name = b.get("sub").toString();
                    QueryWrapper queryWrapper = new QueryWrapper();
                    queryWrapper.eq("user_name", name);
                    UserModel userModel = service.getOne(queryWrapper);
                    if (ObjectUtil.isNotEmpty(userModel)) {
                        QueryWrapper<SiteListModel> siteListModelQueryWrapper = new QueryWrapper<SiteListModel>();
                        siteListModelQueryWrapper.eq("id", ssoModel.getAppid());
                        SiteListModel siteListModel = siteListService.getOne(siteListModelQueryWrapper);
                         if (ObjectUtil.isNotEmpty(siteListModel)){
                             UserSiteModel userSiteModel = new UserSiteModel();
                             userSiteModel.setCreateDate(new Date());
                             userSiteModel.setSiteId(siteListModel.getId().toString());
                             userSiteModel.setIp(ipAddr);
                             userSiteModel.setUserId(userModel.getId());
                             userSiteService.save(userSiteModel);
                             System.out.println( RegexUtils.underlineToHump("get_"+siteListModel.getFileName()));
                             Object Str = ReflectUtil.invoke(userModel, RegexUtils.underlineToHump("get_"+siteListModel.getFileName()));
                             String url = URLDecoder.decode(ssoModel.getRedirecturl() + "&LoginName=" + String.valueOf(Str), "utf-8");
                             response.sendRedirect(url);
                         }else{
                             response.sendRedirect(webUrl + "/403");
                         }
                    } else {
                        response.sendRedirect(webUrl + "/403");
                    }
                } else {
                    response.sendRedirect(webUrl + "/403");
                }
            } else {
                response.sendRedirect(webUrl + "/403");
            }
        } else {
            response.sendRedirect(webUrl + "/403");

        }
    }
}

